﻿@using KMF.WebUI.Areas.KMFadmin.Models
@using KMF.WebUI.Areas.KMFadmin.CustomHelpers
@model IEnumerable<KMF.Common.OrderPagingDto>
@{
    ViewBag.Title = "Danh sách đơn hàng";
    Layout = "~/Areas/KMFadmin/Views/Shared/_layout.cshtml";
}
<link href="~/Areas/KMFadmin/Content/css/browerImage.css" rel="stylesheet" />
<link href="~/Areas/KMFadmin/Scripts/jQueryDatePicker/css/ui-lightness/jquery-ui-1.10.1.custom.min.css" rel="stylesheet" />
<link href="~/Areas/KMFadmin/Scripts/jQueryDatePicker/js/jquery-ui-timepicker-addon.css" rel="stylesheet" />
@section LeftMenu
{
    <div id="left">
        <div class="LeftMenuWraper">
            <div class="leftMenuHeader">
                <span style="padding-left: 10px;">
                    <strong>Truy cập nhanh</strong>
                </span>
            </div>
            <ul id="p-quick-menu" class="quick-menu">
                <li class="qmn-info-pro">
                    <a href="@Url.Action("Index", "AdminCustomer")">Khách hàng</a>
                </li>
                <li class="qmn-seo-pro">
                    <a href="@Url.Action("Index", "AdminOrder")">Đơn hàng</a>
                </li>
            </ul>
            <div class="leftMenuFooter"></div>
        </div>
    </div>
    <!-- END #left -->
}
<nav>
    <div id="jCrumbs" class="breadCrumb module">
        <ul>
            <li><a href="@Url.Action("Index","AdminHome")"><i class="bootstrap icon-home"></i></a></li>
            <li><a href="javascript:void(0)">Danh sách đơn hàng</a> </li>
        </ul>
    </div>
</nav>
<div class="toolBox">
    @*@Html.ToolBox(new List<object> { new Add { ClassCss = "Add", ActionName = "/KMFadmin/AdminOrder/Add" } })*@
</div>
<div class="plcFillter">
    <span>Từ ngày:</span> @Html.TextBox("StartDate", "", new { @style = "width:150px;" })
    <span>Đến ngày:</span> @Html.TextBox("EndDate", "", new { @style = "width:150px;" })
    <input type="submit" value="Lọc" id="btnFilterOder" />
    &nbsp;
    Lọc nhanh: 
    @Html.DropDownList("ddlFilOrder", new SelectList((List<object>)ViewBag.ListOption, "ItemValue", "ItemText"))
</div>
@{
    var OrderNow = from o in Model where o.ReceiveDate.Date >= DateTime.Now.Date orderby o.ReceiveDate descending select o;
    var OrderOld = from o in Model where o.ReceiveDate.Date < DateTime.Now.Date orderby o.ReceiveDate descending select o;
}
<div style="margin-top:20px;">
    <strong>Các đơn hàng hôm nay</strong>
</div>
<table class="table-odd table-plist">
    <tr>
        <th style="width:30px;">STT</th>
        <th style="width:80px;">Mã</th>
        <th>Người đặt</th>
        <th style="width:100px;">Ngày đặt</th>
        <th style="width:100px;">Tổng tiền</th>
        <th style="width:60px;">Trạng thái</th>
    </tr>
    @if (OrderNow.Any())
    {
        foreach (var item in OrderNow)
        {
            <tr>
                <td>@item.STT</td>
                <td>@item.OrderId</td>
                <td>
                    <a href="">@item.FullName</a>
                </td>
                <td>
                    @item.ReceiveDate.ToString("dd/MM/yyyy")
                </td>
                <td>@item.TotalMoney.ToString("##,###")</td>
                <td style="text-align:center;">
                    <a href="@Url.Action("Detail", "AdminOrder", new { id = @item.OrderId })">Chi tiết</a>
                </td>
            </tr>
        }
    }
</table>
<div style="margin-top:30px;">
    <strong>Các đơn hàng cũ</strong>
</div>
<table class="table-odd table-plist">
    <tr>
        <th style="width:30px;">STT</th>
        <th style="width:80px;">Mã</th>
        <th>Người đặt</th>
        <th style="width:100px;">Ngày đặt</th>
        <th style="width:100px;">Tổng tiền</th>
        <th style="width:60px;">Trạng thái</th>
    </tr>
    @if (Model.Any())
    {
        foreach (var item2 in OrderOld)
        {
            <tr>
                <td>@item2.STT</td>
                <td>@item2.OrderId</td>
                <td>
                    <a href="">@item2.FullName</a>
                </td>
                <td>
                    @item2.ReceiveDate.ToString("dd/MM/yyyy")
                </td>
                <td>@item2.TotalMoney.ToString("##,###")</td>
                <td style="text-align:center;">
                    <a href="@Url.Action("Detail", "AdminOrder", new { id = @item2.OrderId })">Chi tiết</a>
                </td>
            </tr>
        }
    }
</table>
<div class="p-paging">
    @Html.PageLinks((PageInfo)ViewBag.PageInfo, i => Url.Action("Index", "AdminOrder", new { page = i }))
</div>
@section scripts
{
    @Scripts.Render("~/bundles/KMFAdmin/jqueryval")
    <script src="@Url.Content("~/Areas/KMFadmin/Scripts/jQueryDatePicker/js/jquery-ui-1.10.1.custom.min.js")"></script>
    <script src="@Url.Content("~/Areas/KMFadmin/Scripts/jQueryDatePicker/js/jquery-ui-timepicker-addon.js")"></script>
    <script type="text/javascript">
        $('#btnFilterOder').click(function () {
            var s = $('.plcFillter #StartDate').val();
            var e = $('.plcFillter #EndDate').val();
            if (s != '' && e != '') {
                location.href = '/KMFadmin/AdminOrder/Filter/?StartDate=' + s + '&EndDate=' + e;
            }
        });

        $('select#ddlFilOrder').change(function () {
            var data = $(this).val();
            //ngay hien tai
            var e = new Date(); //nay ket thuc
            var eDate = e.getDate() + '/' + (e.getMonth() + 1) + '/' + e.getFullYear();
            var s = new Date(); //ngay bat dau
            var param = '';
            switch (data) {
                case 'today':
                    param += '?StartDate=' + eDate + '&EndDate=' + eDate;
                    break;
                case 'yesterday':
                    s.setDate(s.getDate() - 1);
                    e.setDate(e.getDate() - 1);
                    var sDate = s.getDate() + '/' + (s.getMonth() + 1) + '/' + s.getFullYear();
                    eDate = e.getDate() + '/' + (e.getMonth() + 1) + '/' + e.getFullYear();
                    param += '?StartDate=' + sDate + '&EndDate=' + eDate;
                    break;
                case 'Lastweek':
                    s.setDate(s.getDate() - 7);
                    var sDate = s.getDate() + '/' + (s.getMonth() + 1) + '/' + s.getFullYear();
                    param += '?StartDate=' + sDate + '&EndDate=' + eDate;
                    break;
                case 'LastMonth':
                    e.setDate(e.getMonth() - 2);
                    var eDate = e.getDate() + '/' + (e.getMonth() + 1) + '/' + e.getFullYear();
                    var sDate = '01/' + (e.getMonth() + 1) + '/' + s.getFullYear();
                    param += '?StartDate=' + sDate + '&EndDate=' + eDate;
                    break;
            }

            if (data == '0') {
                location.href = '/KMFadmin/AdminOrder/index';
            } else {

                location.href = '/KMFadmin/AdminOrder/Filter/' + param;
            }
        });

        function DeleteOrder(id) {
            $.ajax({
                url: '/KMFadmin/AdminOrder/DeleteOrder',
                data: { id: id },
                success: function (data) {
                    if (data) {
                        alert('Xóa khách hàng thành công!');
                        window.location = '/KMFadmin/AdminOrder';
                    } else {
                        alert('Xóa khách hàng thất bại!');
                    }
                },
                error: function () {
                    alert('Đã có lỗi xảy ra!');
                }
            });
        }
        // ------------- Date picker -------
        $('#StartDate').datetimepicker({ dateFormat: 'dd/mm/yy', timeFormat: "hh:mm:ss tt", timeOnlyTitle: "Chọn thời gian", showTimepicker: false });
        $('#EndDate').datetimepicker({ dateFormat: 'dd/mm/yy', timeFormat: "hh:mm:ss tt", timeOnlyTitle: "Chọn thời gian", showTimepicker: false });
    </script>
}
